package com.gwclh.personnel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gwclh.personnel.bean.ResultVo;
import cn.hutool.core.lang.Assert;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Department;
import com.gwclh.personnel.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@RestController
@Api(tags = "部门相关接口")
@RequestMapping("/departments")
public class DepartmentController extends BaseController {

  @Autowired
  IDepartmentService departmentService;

  @GetMapping("/{id}")
  @ApiOperation(value = "根据id获取部门信息")
  public ResultVo getDeptById(@PathVariable Integer id) {
    Department department = departmentService.getById(id);
    return ResultVo.success(department);
  }


  @GetMapping("tree")
  @ApiOperation(value = "获取部门列表树形结构")
  public ResultVo listTreeDept(Department department) {
    return departmentService.listDeptTree(department);
  }

  @GetMapping("")
  @ApiOperation(value = "分页查询部门列表")
  public ResultVo listDeptByPage(Department department, HttpServletRequest request) {
    // 根据请求获取分页信息
    PageDomain pageDomain = getPageParams(request);
    return departmentService.listDepartments(pageDomain, department);
  }

  @PostMapping("")
  @ApiOperation(value = "添加部门")
  public ResultVo insertUser(@Validated @RequestBody Department department) {
    return departmentService.insertDepartment(department);
  }

  @DeleteMapping("/{id}")
  @ApiOperation(value = "删除单一部门")
  public ResultVo delDept(@PathVariable(value = "id") Integer id) {
    return departmentService.delDept(id);
  }

  @PutMapping("")
  @ApiOperation(value = "更新部门信息")
  public ResultVo mdfDept(@Validated @RequestBody Department department) {
    Department department1 = departmentService.getById(department.getId());
//    Assert.isNull(department1, "部门已存在");
    departmentService.updateById(department);
    return ResultVo.success(department);
  }

}
